/*
 * AGENT SHERPA
 *  
 * Assignement 2 of the Agent Oriented Software Engineering Course
 * M.Sc. in Advanced Software Engineering (Part-time)
 * University College Dublin www.ucd.ie
 *
 * Copyright (c) 2007 Chris Walsh, Aidan Morrisy and Iain Hull.
 * All rights reserved.
 * 
 * Filename: Main.aps
 * Author:   iain
 * Date:     06 February 2008
 */

package org.agentsherpa.scratch;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.agentsherpa.model.Point;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class MapModelImpl implements MapModel {
    private List<Point> points = new ArrayList<Point>();

    private Point dimension = null;

    public MapModelImpl(URL source) {
        DocumentBuilder builder;
        try {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            Document doc = builder.parse(new InputSource(source.openStream()));

            for (Node d = doc.getDocumentElement().getFirstChild(); d != null; d = d
                    .getNextSibling()) {
                if (d instanceof Element) {
                    Element e = (Element) d;

                    if ("size".endsWith(e.getNodeName())) {
                        int x = Integer.valueOf(e.getAttribute("width"));
                        int y = Integer.valueOf(e.getAttribute("height"));
                        dimension = new Point(x, y);
                    }
                    else if ("points".endsWith(e.getNodeName())) {
                        for (Node d2 = doc.getDocumentElement().getFirstChild(); d2 != null; d2 = d2
                                .getNextSibling()) {
                            if (d2 instanceof Element) {
                                Element e2 = (Element) d2;
                                if ("point".endsWith(e.getNodeName())) {
                                    int x = Integer
                                            .valueOf(e.getAttribute("x"));
                                    int y = Integer
                                            .valueOf(e.getAttribute("y"));
                                    Point p = new Point(x, y);
                                    points.add(p);
                                }
                            }
                        }

                    }
                }
            }
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Iterator<Point> getPoints() {
        return points.iterator();
    }

}
